﻿
package com.ostrich.project.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.DateUtil;
import com.base.util.HtmlUtil;
import com.base.util.SessionUtils;
import com.base.web.BaseAction;
import com.ostrich.manpower.bean.TostHrAccfundinfo;
import com.ostrich.manpower.bean.TostHrDepartinfo;
import com.ostrich.manpower.common.FormulaUtil;
import com.ostrich.project.bean.TostPjCostitem;
import com.ostrich.project.bean.TostPjOfficedetail;
import com.ostrich.project.bean.TostPjProjcttab;
import com.ostrich.project.service.impl.TostPjCostitemService;
import com.ostrich.project.service.impl.TostPjOfficedetailService;
import com.ostrich.project.service.impl.TostPjProjcttabService;

/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * 
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostPjOfficedetail")
public class TostPjOfficedetailController extends BaseAction
{
	private final static Logger log= Logger.getLogger(TostPjOfficedetailController.class);

	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostPjOfficedetailService<TostPjOfficedetail> tostPjOfficedetailService; 
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostPjProjcttabService<TostPjProjcttab> tostPjProjcttabService;
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostPjCostitemService<TostPjCostitem> tostPjCostitemService;
	/**
	 * ilook 首页
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list") 
	public ModelAndView  list(TostPjOfficedetail tostPjOfficedetail,HttpServletRequest request,HttpSession session) throws Exception{
		Map<String,Object>  context = getRootMap();
		
		//判断页面是否直接跳转add的form表单的展示界面
		Integer count = tostPjOfficedetailService.queryByCount(tostPjOfficedetail);
		// 判断页面是否直接跳转add的form表单的展示界面
		String flag = (count<=0)? "add": "list";
		
		session.setAttribute("pjid",tostPjOfficedetail.getPjId());
		session.setAttribute("pctid",tostPjOfficedetail.getPctId());
		TostPjProjcttab pjinfo = tostPjProjcttabService.queryById(tostPjOfficedetail.getPjId());
		TostPjCostitem pjcost = tostPjCostitemService.queryById(tostPjOfficedetail.getPctId());
		//List<TostPjOfficedetail> dataList = tostPjOfficedetailService.queryByList(tostPjOfficedetail);
		//设置页面数据
		//context.put("dataList", dataList);
		context.put("flag", flag);
		context.put("pushmy", tostPjOfficedetail);
		context.put("pjinfo",pjinfo);
		context.put("pjcost",pjcost);
		return forword("project/tabTostPjOfficedetail",context); 
	}
	
	
	
	/**
	 * json 列表页面
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/dataList") 
	public void  dataList(TostPjOfficedetail tostPjOfficedetail,HttpServletResponse response,HttpSession session) throws Exception{
		Integer pjid = (Integer)session.getAttribute("pjid");
		Integer pctid = (Integer)session.getAttribute("pctid");
		tostPjOfficedetail.setPjId(pjid);
		tostPjOfficedetail.setPctId(pctid);
		List<TostPjOfficedetail> dataList = tostPjOfficedetailService.queryByList(tostPjOfficedetail);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",tostPjOfficedetail.getPager().getRowCount());
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}

	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 
	@RequestMapping("/save")
	public void save(TostPjOfficedetail bean,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = new HashMap<String,Object>();
		tostPjOfficedetailService.updateBySelective(bean);
		sendSuccessMessage(response, "保存成功~");
	}*/
	
	@RequestMapping("/save")
	public void save(HttpSession session,HttpServletRequest request,TostPjOfficedetail tostPjOfficedetail,HttpServletResponse response) throws Exception{
		Integer pjid = (Integer)session.getAttribute("pjid");
		Integer pctid = (Integer)session.getAttribute("pctid");
		tostPjOfficedetail.setPjId(pjid);
		tostPjOfficedetail.setPctId(pctid);
		
		String podCash=request.getParameter("podCash");
		
		Map<String, Object> offMap = FormulaUtil.formulaById(15, Float.valueOf(podCash));
		tostPjOfficedetail.setPodAvgcost((Float)offMap.get("officepay"));
		//tostPjOfficedetail.setPodAvgcost(new Float(999));
		if(tostPjOfficedetail.getId() == null){
			// 获取从页面传来的session(用户名)
			tostPjOfficedetail.setCreatetime(DateUtil.getNowPlusTime());
			tostPjOfficedetail.setPerson(SessionUtils.getUser(request).getEmail());
			tostPjOfficedetailService.add(tostPjOfficedetail);
		}else{
			tostPjOfficedetailService.update(tostPjOfficedetail);
		}
		sendSuccessMessage(response, "保存成功~");
	}
	
	
	
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = getRootMap();
		TostPjOfficedetail bean  = tostPjOfficedetailService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletResponse response) throws Exception{
		tostPjOfficedetailService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}
	
	@RequestMapping("/getProjectInfo")
	public void queryProjectInfo(HttpServletResponse response){
		List<TostPjProjcttab> dataList =tostPjProjcttabService.queryProjct_to_others() ;
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",(dataList.size()==0)?0:dataList.size());
		jsonMap.put("data", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
	@RequestMapping("/getCostitemInfo")
	public void queryCostitemInfo(HttpServletResponse response){
		List<TostPjCostitem> dataList =tostPjCostitemService.queryCostitem_to_others() ;
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",(dataList.size()==0)?0:dataList.size());
		jsonMap.put("data", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}
}
